package org.bt.common.validator.notempty;

import org.bt.common.validator.aspect.NotEmpty;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Check that the character sequence is not null and its length is strictly superior to 0.
 *
 * @author tongw
 */
public class NotEmptyValidatorForCharSequence implements ConstraintValidator<NotEmpty, CharSequence> {

    /**
     * Checks the character sequence is not {@code null} and not empty.
     *
     * @param charSequence               the character sequence to validate
     * @param constraintValidatorContext context in which the constraint is evaluated
     * @return returns {@code true} if the character sequence is not {@code null} and not empty.
     */
    @Override
    public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {
        if (charSequence == null) {
            return false;
        }
        return charSequence.length() > 0;
    }
}
